if(!require(ggplot2))install.packages" />

Learn R 画图1

您所在的位置:网站首页 ggplot2 画图 Learn R 画图1

Learn R 画图1

2023-03-29 21:09| 来源: 网络整理| 查看: 265

R包下载设置镜像>options("repos"=c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")) #安装R包 >if(!require(ggplot2))install.packages('ggplot2',update = F,ask = F) >if(!require(ggpubr))install.packages('ggpubr',update = F,ask = F) >if(!require(eoffice))install.packages("eoffice",update = F,ask = F) >if(!require(patchwork))install.packages("patchwork",update = F,ask = F) #加载以检查是否安装成功 >library(ggplot2) >library(ggpubr) >library(eoffice) >library(patchwork)常用的可视化R包1.作图 2.拼图 3.导出常用的可视化R包基础包(basese)绘图函数基础包(basese)绘图函数1.基础包> plot(iris[,1],iris[,3],col = iris[,5]) #x轴-iris[,1],y轴-iris[,3] > text(6.5,4, labels = 'hello') #6.5,4-坐标 labels = 'xxx' 添加标签 >dev.off() #关闭画板#1.基础包2.ggplot2 中坚力量,语法有个性> library(ggplot2) > ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species))#2.ggplot23.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一> library(ggpubr) > ggscatter(iris, + x="Sepal.Length", + y="Petal.Length", + color="Species")ggpubr 新手友好型ggplot21.入门级绘图模板:作图数据,横纵坐标ggplot2代码模板>library(ggplot2) >ggplot(data = )+ (mapping=aes())

#代表着模版不是具体的代码

#代表画图的函数

#写横纵坐标 列名不带“”

#“+”代表ggplot()和geom_point()两个函数是同一张图

#ggplot2的特殊语法:列名不加引号,行末写加号

ggplot例子> ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length))2.属性设置(颜色、大小、透明度、点的形状,线型等)2.1 手动设置,需要设置为有意义的值> ggplot(data = iris) + + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length), + color = "blue")>ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), size = 5, # 点的大小5mm alpha = 0.5, # 透明度 50% shape = 8) # 点的形状2.2 映射:按照数据框的某一列来定义图的某个属性,不是手动设置> ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species)) > ggplot(data = iris)+ ## Q1 能不能自行指定映射的具体颜色? + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species))+ scale_color_manual(values = c("blue","grey","red"))映射VS.手动设置·映射根据数据的某一列的内容分配颜色·手动设置:把图形设置为1个或者n个颜色,于数据内容无关#映射 aes() >ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species) #这个color是aes的参数,aes是不带引号的,存在于数据中的列名 ····映射 #手动设置 > ggplot(data = iris) + + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length), + color = "blue") #这个color是geom_point的参数,是具体的颜色···手动设置Q2 区分color和fill两个属性Q2-1 空心形状和实心形状都用color设置颜色> ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species), + shape = 17) #shape=17号,实心的例子 > ggplot(data = iris)+ + geom_point(mapping = aes(x = Sepal.Length, + y = Petal.Length, + color = Species), + shape = 2) #shape=2号,空心的例子练习6-11.加载test.Rdata,分别test的以a和b列作为横纵坐标,change列映射颜色,画点图。2.尝试修改点的颜色为暗绿色(darkgreen)、灰色、红色ggplot(data = test)+geom_point(mapping = aes(x=a, y=b, color=change)) ggplot(data = test)+geom_point(mapping = aes(x=a, y=b, color=change))+ scale_color_manual(values = c("darkgreen","grey","red"))ggplot 分面1.单分面ggplot(data = iris) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + facet_wrap(~ Species) #分面代码,根据species的取值分开ggplot单分面2.双分面#原本该数据中是没有group这一组的 dat = iris #不推荐用data做变量名称,data是个函数 dat$Group = sample(letters[1:5],150,replace = T) #sample(letters[1:5],150,replace = T) #sample()函数的意思是随机抽样,默认replace = F即不放回取样,无重复值 ggplot(data = dat) + geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + facet_grid(Group ~ Species) #group取值是横着的,species是竖着的sample()函数ggplot 双分面4.几何对象

#一个函数生成的所有的点所组成的图像,可用加号进行叠加

ggplot(data = iris) + geom_smooth(mapping = aes(x = Sepal.Length, y = Petal.Length))+ geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) #两个图层叠在一起 ——局部设置 仅对当前图层有效 ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+ geom_smooth()+ geom_point() #两个图层叠在一起 ——全局设置 对所有图层有效几何对象5.统计变换-直方图View(diamonds) table(diamonds$cut) #统计取值 ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut)) ggplot(data = diamonds) + stat_count(mapping = aes(x = cut)) #这两个函数效果一样geom开头是画图函数,stat开头是统计变换函数 #x=cut是将cut作为横坐标,默认统计数为纵坐标,不用写y=统计变换使用场景

#5.1.不统计,是用表里的数据直接做图

fre = as.data.frame(table(diamonds$cut)) fre ggplot(data = fre) + geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity") #自己想要写纵坐标 必须写上stat = "identity"

#5.2count改为prop(比例)

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) #group=1意思是各自占总比多少5.2count改为prop练习6-2ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, color = Species))+ geom_boxplot() ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species)) + #color=换成fill=空心变实心 geom_boxplot()+ geom_point()练习6-2叠加过点图之后,但是真实样本下有50个样,该图把相近的值合并在一起了,真正的应为图1#图一正确函数 geom_jitter()图16.位置关系6.2堆叠直方图ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut,fill=clarity))堆叠直方图6.3 并列直方图ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")并列直方图7.坐标系

#翻转coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()翻转/极坐标系

#极坐标系coord_polar()

bar ggscatter(iris,x="Sepal.Length", + y="Petal.Length", + color="Species") > p p > my_comparisons p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value + stat_compare_means(label.y = 9) #指定标签显示的Y轴位置,可使用参数label.y=ggpubr加p值线图片保存的三种方法1.通用基础包作图的保存pdf("iris_box_ggpubr.pdf") #保存的格式及文件名 boxplot(iris[,1]~iris[,5]) text(6.5,4, labels = 'hello') dev.off() #画完了,关闭画板2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave()p


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3